import 'package:flutter/material.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:xinxuan/common/address_bar.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/goods_order_info_item.dart';
import 'package:xinxuan/common/network_image_util.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/controllers/my_controller/gift_controller/gift_order_detail_page_controller.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/utils/color_util.dart';

class GiftOrderDetailPage extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: '订单详情',
        backgroundColor: pageBgColor,
      ),
      body: GetBuilder<GiftOrderDetailPageController>(
        init: GiftOrderDetailPageController(),
        builder: (controller) {
          if (controller.giftOrderItem.value.id! <= 0) {
            return Container();
          }
          return ScrollConfigurationCommon(child: ListView(
            children: [
              SizedBox(height: 16.h),
              AddressBar(
                consignee: controller.giftOrderItem.value.address!.consignee ?? '', 
                mobile: controller.giftOrderItem.value.address!.mobile ?? '', 
                provinceStr: controller.giftOrderItem.value.address!.provinceStr ?? '', 
                cityStr: controller.giftOrderItem.value.address!.cityStr ?? '', 
                districtStr: controller.giftOrderItem.value.address!.districtStr ?? '', 
                address: controller.giftOrderItem.value.address!.address ?? '',
                id: 1,
                showRight: false,
              ),
              Container(
                margin: EdgeInsets.only(top: 12.h),
                padding: EdgeInsets.symmetric(horizontal: 12.w),
                color: bgWhiteColor,
                  child: Column(
                  children: [
                    Container(
                      height: 44.h,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '魔法星选直营',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: textBlackColor,
                          fontWeight: FontWeight.bold
                        ),
                      ),
                    ),
                    _goodArrBody(controller)
                  ],
                ),
              ),
              Container(
                padding: EdgeInsets.symmetric(vertical: 8.h),
                margin: EdgeInsets.only(top: 12.h),
                color: bgWhiteColor,
                child: Column(
                  children: [
                    GoodsOrderInfoItem(title: '订单编号', value: '${controller.giftOrderItem.value.orderSn}', showCopy: true),
                    GoodsOrderInfoItem(title: '创建时间', value: '${controller.giftOrderItem.value.createdAt}'),
                  ],
                ),
              ),
              Container(
                margin: EdgeInsets.only(top: 12.h),
                color: bgWhiteColor,
                child: Column(
                  children: [
                    Container(
                      padding: EdgeInsets.symmetric(vertical: 8.h),
                      child: Column(
                        children: [
                          GoodsOrderInfoItem(title: '商品金额', value: '¥ ${controller.giftOrderItem.value.totalAmount}', valueBold: true),
                          GoodsOrderInfoItem(title: '减免金额', value: '- ¥ 0.00', valueBold: true),
                          GoodsOrderInfoItem(title: '物流类型', value: '${controller.giftOrderItem.value.expressCompany}', valueBold: true),
                          GoodsOrderInfoItem(title: '运费', value: '${controller.giftOrderItem.value.expressPrice}', valueBold: true),
                        ],
                      ),
                    ),
                    Container(
                      height: 49.h,
                      alignment: Alignment.centerRight,
                      padding: EdgeInsets.symmetric(horizontal: 16.w),
                      decoration: BoxDecoration(
                        border: Border(
                          top: BorderSide(
                            color: lineColor,
                            width: .5
                          )
                        )
                      ),
                      child: Text.rich(
                        TextSpan(
                          text: '应付款: ',
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 14.sp
                          ),
                          children: [
                            TextSpan(
                              text: '¥',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 14.sp,
                                fontWeight: FontWeight.bold
                              ),
                            ),
                            TextSpan(
                              text: '${controller.giftOrderItem.value.payAmount}',
                              style: TextStyle(
                                color: mainTextColor,
                                fontSize: 18.sp,
                                fontWeight: FontWeight.bold
                              ),
                            )
                          ]
                        )
                      ),
                    )
                  ],
                ),
              )
            ],
          ));
        },
      ),
    );
  }

  /// 商品列表
  Widget _goodArrBody(GiftOrderDetailPageController controller) {
    return Column(
      children: controller.giftOrderItem.value.products!.map((productItem) {
        return Container(
          margin: EdgeInsets.only(bottom: 12.h),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              NetworkImageUtil(
                url: productItem.image ?? '', 
                width: 90.w, 
                height: 90.w,
                borderRadius: 5.w,
              ),
              SizedBox(width: 12.w),
              Expanded(child: Container(
                constraints: BoxConstraints(
                  minHeight: 90.w,
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Expanded(child: Text(
                          '${productItem.title}',
                          maxLines: 2,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            color: textBlackColor,
                            fontSize: 14.sp
                          ),
                        )),
                        SizedBox(width: 6.w),
                        Text(
                          'x${productItem.quantity}',
                          style: TextStyle(
                            color: textSubtitleColor,
                            fontSize: 12.sp
                          ),
                        ),
                      ],
                    ),
                    SizedBox(height: 6.h),
                    Text(
                      '单价: ${productItem.price}',
                      style: TextStyle(
                        color: textSubtitleColor,
                        fontSize: 12.sp
                      ),
                    )
                  ],
                ),
              )),
            ],
          ),
        );
      }).toList(),
    );
  }
  
}